package com.wwwarehouse.contract.contract.modify;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.InfoCardOrderDetailBean;
import com.wwwarehouse.contract.bean.SettingRuleBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * @author feng.gao on 2017/11/8.
 *         <p/>
 *         已发布商品列表
 */

public class IssuedGoodsAdapter extends RecyclerView.Adapter<IssuedGoodsAdapter.ViewHolder> {

	private Context mContext = null;
	private int mHeight = 0;
	private OnItemClickListener mListener = null;
	private List<InfoCardOrderDetailBean.ItemDetailBean> tagList = new ArrayList<>();
	private showPopListener mShowPopListener;
	private clickRuleListener mClickRuleListener;

	public IssuedGoodsAdapter(Context context, List<InfoCardOrderDetailBean.ItemDetailBean> list, int height) {
		this.mContext = context;
		this.mHeight = height;
		this.tagList = list;
	}

	public void setPopUPWindowListener(showPopListener mShowPopListener) {
		this.mShowPopListener = mShowPopListener;
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.mListener = listener;
	}

	public void setOnClickRuleListener(clickRuleListener mClickRuleListener) {
		this.mClickRuleListener = mClickRuleListener;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contract_issuedgoods_adapter_setting_goods_rule_adapter, parent, false);
		ViewHolder vh = new ViewHolder(view);
		return vh;

	}

	@Override
	public void onBindViewHolder(final ViewHolder holder, final int position) {
		if (tagList == null || tagList.size() <= 0) {
			return;
		}
		GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
		params.height = (mHeight - ConvertUtils.dip2px(mContext, 60)) / 5; //RecyclerView高度减去距离按钮的高度
		holder.itemView.setLayoutParams(params);
		holder.tvGoodName.setText(tagList.get(position).getName());
		ImageloaderUtils.displayRoundImg(tagList.get(position).getImg(), holder.ivGoodsPic);
		if (tagList.get(position).getQty() != null) {
			holder.mGoodsSale.setText(String.format("库存:%s", tagList.get(position).getQty()));
		} else {
			holder.mGoodsSale.setText("库存:");
		}
		if (StringUtils.isNoneNullString(tagList.get(position).getPrice())) {
			holder.tvDetails.setText("¥" + tagList.get(position).getPrice() + "");
		} else {
			holder.tvDetails.setText("¥");
		}
		holder.mCode.setText(tagList.get(position).getBarCode());
		if (tagList.get(position).getPriceRuleType() != null) {
			tagList.get(position).setHasSetRule(true);
		}
		holder.mSetRule.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mClickRuleListener.RuleClick(position);
			}
		});

		holder.itemView.setTag(tagList.get(position));
		holder.mPoints.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mShowPopListener.showPopWindow(holder.rlDelete, position);
			}
		});
		holder.rlDelete.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				mShowPopListener.showPopWindow(holder.rlDelete, position);
				return true;
			}
		});
	}


	@Override
	public int getItemCount() {
		return tagList.size();
	}

	public class ViewHolder extends RecyclerView.ViewHolder {
		TextView tvGoodName;
		TextView mGoodsSale;
		TextView tvDetails;
		ImageView ivGoodsPic;
		TextView mSetRule;
		TextView mCode;
		RelativeLayout rlDelete;
		ImageView mPoints;

		public ViewHolder(View view) {
			super(view);
			tvGoodName = (TextView) view.findViewById(R.id.tv_goods_name);
			mGoodsSale = (TextView) view.findViewById(R.id.tv_goods_sale);
			tvDetails = (TextView) view.findViewById(R.id.tv_shop_name);
			ivGoodsPic = (ImageView) view.findViewById(R.id.iv_goods_pic);
			mCode = (TextView) view.findViewById(R.id.tv_goods_code);
			mSetRule = (TextView) view.findViewById(R.id.img_edit);
			rlDelete = (RelativeLayout) view.findViewById(R.id.rl_delete);
			mPoints = (ImageView) view.findViewById(R.id.img_points);
		}
	}

	public interface OnItemClickListener {
		void ItemClickListener(List<SettingRuleBean.DataBean.ListBean> tagList);
	}

	public interface showPopListener {
		void showPopWindow(View view, int position);
	}

	public interface clickRuleListener {
		void RuleClick(int position);
	}
}
